package lt.mockos.app.client.cpu.view;

import lt.mockos.app.client.cpu.presenter.CPUPresenter.CPUDisplay;
import lt.mockos.app.client.widget.MemTable;
import lt.mockos.app.client.widget.TextField;
import lt.mockos.app.client.widget.VMControls;

import com.google.gwt.dom.client.Style.BorderStyle;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class CPUView extends Composite implements CPUDisplay {

    private TextField CS = new TextField("CS");
    private TextField DS = new TextField("DS");
    private TextField SS = new TextField("SS");

    private TextField CP = new TextField("CP");
    private TextField SP = new TextField("SP");

    private TextField AR = new TextField("AR");
    private TextField BR = new TextField("BR");

    private TextField SF = new TextField("SF");

    private TextField TIME = new TextField("TIME");
    private TextField MODE = new TextField("MODE");
    private TextField PTR = new TextField("PTR");

    private TextField SI = new TextField("SI");
    private TextField PI = new TextField("PI");
    private TextField TI = new TextField("TI");

    private TextField CH1 = new TextField("CH1");
    private TextField CH2 = new TextField("CH2");
    private TextField CH3 = new TextField("CH3");

    private MemTable userMem = new MemTable(33, 10);
    private MemTable superMem = new MemTable(17, 10);

    private Button enterBtn = new Button("Enter");

    private Button loadBtn = new Button("Load");
    private Button unloadBtn = new Button("Unload");
    private Button runBtn = new Button("Run");
    private Button stopBtn = new Button("Stop");
    private Button stepBtn = new Button("Step");
    private CheckBox stepMode = new CheckBox("Step mode");

    private TextArea task = new TextArea();

    private static final String SPACE = "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";

    private TextArea output = new TextArea();

    private TextBox input = new TextBox();

    private VMControls vmControls = new VMControls();

    public CPUView() {

        FlexTable regTable = new FlexTable();

        int row = 0;
        int col = 0;
        regTable.setWidget(row, col + 0, CS.getLabel());
        regTable.setWidget(row, col + 1, CS);
        row++;
        regTable.setWidget(row, col + 0, DS.getLabel());
        regTable.setWidget(row, col + 1, DS);
        row++;
        regTable.setWidget(row, col + 0, SS.getLabel());
        regTable.setWidget(row, col + 1, SS);
        row++;
        regTable.setWidget(row, col + 0, CP.getLabel());
        regTable.setWidget(row, col + 1, CP);
        row++;
        regTable.setWidget(row, col + 0, SP.getLabel());
        regTable.setWidget(row, col + 1, SP);
        row++;
        regTable.setWidget(row, col + 0, AR.getLabel());
        regTable.setWidget(row, col + 1, AR);

        regTable.setWidget(0, col + 2, new HTMLPanel(SPACE));
        row = 0;
        col = 3;

        regTable.setWidget(row, col + 0, BR.getLabel());
        regTable.setWidget(row, col + 1, BR);
        row++;
        regTable.setWidget(row, col + 0, SF.getLabel());
        regTable.setWidget(row, col + 1, SF);
        row++;
        regTable.setWidget(row, col + 0, TIME.getLabel());
        regTable.setWidget(row, col + 1, TIME);
        row++;
        regTable.setWidget(row, col + 0, MODE.getLabel());
        regTable.setWidget(row, col + 1, MODE);
        row++;
        regTable.setWidget(row, col + 0, PTR.getLabel());
        regTable.setWidget(row, col + 1, PTR);

        regTable.setWidget(0, col + 2, new HTMLPanel(SPACE));
        row = 0;
        col = 6;

        regTable.setWidget(row, col + 0, SI.getLabel());
        regTable.setWidget(row, col + 1, SI);
        row++;
        regTable.setWidget(row, col + 0, PI.getLabel());
        regTable.setWidget(row, col + 1, PI);
        row++;
        regTable.setWidget(row, col + 0, TI.getLabel());
        regTable.setWidget(row, col + 1, TI);
        row++;
        // regTable.setWidget(row, col + 0, CH1.getLabel());
        // regTable.setWidget(row, col + 1, CH1);
        // row++;
        // regTable.setWidget(row, col + 0, CH2.getLabel());
        // regTable.setWidget(row, col + 1, CH2);
        // row++;
        // regTable.setWidget(row, col + 0, CH3.getLabel());
        // regTable.setWidget(row, col + 1, CH3);

        regTable.setCellPadding(0);
        regTable.setCellSpacing(3);
        HorizontalPanel layout = new HorizontalPanel();
        initWidget(layout);

        output.setWidth("330px");
        output.setHeight("100px");
        output.setReadOnly(true);
        input.setWidth("276px");

        CaptionPanel registers = new CaptionPanel("Registers");
        CaptionPanel userMemmory = new CaptionPanel("User memmory");
        CaptionPanel superUserMemmory = new CaptionPanel("Super user memmory");
        CaptionPanel ioPanel = new CaptionPanel("Input/Output");

        registers.add(regTable);
        userMemmory.add(userMem);
        superUserMemmory.add(superMem);

        VerticalPanel vp2 = new VerticalPanel();
        vp2.add(output);
        FlowPanel fp = new FlowPanel();
        fp.add(input);
        fp.add(enterBtn);
        vp2.add(fp);

        input.setMaxLength(4);

        input.setEnabled(false);

        enterBtn.setEnabled(false);
        unloadBtn.setEnabled(false);

        ioPanel.add(vp2);

        VerticalPanel vp = new VerticalPanel();
        vp.add(registers);
        registers.setHeight("194px");
        vp.add(superUserMemmory);
        vp.add(ioPanel);

        VerticalPanel vp3 = new VerticalPanel();
        vp3.add(userMemmory);

        CaptionPanel taskPanel = new CaptionPanel("Task");
        task.setWidth("350px");
        task.setHeight("607px");
        taskPanel.add(task);

        CaptionPanel controlsPanel = new CaptionPanel("Controls");
        Grid g = new Grid(3, 2);
        HorizontalPanel hp = new HorizontalPanel();

        g.setWidget(0, 0, loadBtn);
        g.setWidget(0, 1, unloadBtn);
        g.setWidget(1, 0, runBtn);
        g.setWidget(1, 1, stopBtn);
        g.setWidget(2, 0, stepBtn);
        g.setWidget(2, 1, stepMode);
        g.setCellPadding(7);
        g.getElement().getStyle().setBorderStyle(BorderStyle.SOLID);
        g.getElement().getStyle().setBorderColor("#ccc");
        g.getElement().getStyle().setBorderWidth(1.0, Unit.PX);

        hp.add(vmControls);
        hp.add(g);
        hp.setCellHorizontalAlignment(g, HasHorizontalAlignment.ALIGN_RIGHT);
        hp.setWidth("100%");
        vmControls.setHeight("128px");
        controlsPanel.add(hp);
        vp3.add(controlsPanel);

        stepMode.setValue(false);
        stopBtn.setEnabled(false);
        stepBtn.setEnabled(false);
        runBtn.setEnabled(false);

        layout.add(vp);
        layout.add(vp3);
        layout.add(taskPanel);

    }

    @Override
    public TextField getCS() {
        return CS;
    }

    @Override
    public TextField getDS() {
        return DS;
    }

    @Override
    public TextField getSS() {
        return SS;
    }

    @Override
    public TextField getCP() {
        return CP;
    }

    @Override
    public TextField getSP() {
        return SP;
    }

    @Override
    public TextField getAR() {
        return AR;
    }

    @Override
    public TextField getBR() {
        return BR;
    }

    @Override
    public TextField getSF() {
        return SF;
    }

    @Override
    public TextField getTIME() {
        return TIME;
    }

    @Override
    public TextField getMODE() {
        return MODE;
    }

    @Override
    public TextField getPTR() {
        return PTR;
    }

    @Override
    public TextField getSI() {
        return SI;
    }

    @Override
    public TextField getPI() {
        return PI;
    }

    @Override
    public TextField getTI() {
        return TI;
    }

    @Override
    public TextField getCH1() {
        return CH1;
    }

    @Override
    public TextField getCH2() {
        return CH2;
    }

    @Override
    public TextField getCH3() {
        return CH3;
    }

    @Override
    public void setCH3(TextField cH3) {
        CH3 = cH3;
    }

    @Override
    public MemTable getUserMem() {
        return userMem;
    }

    @Override
    public MemTable getSuperMem() {
        return superMem;
    }

    @Override
    public Button getEnterBtn() {
        return enterBtn;
    }

    @Override
    public Button getRunBtn() {
        return runBtn;
    }

    @Override
    public Button getStepBtn() {
        return stepBtn;
    }

    @Override
    public Button getStopBtn() {
        return stopBtn;
    }

    @Override
    public CheckBox getStepMode() {
        return stepMode;
    }

    @Override
    public TextArea getTask() {
        return task;
    }

    @Override
    public TextArea getOutput() {
        return output;
    }

    @Override
    public TextBox getInput() {
        return input;
    }

    @Override
    public Button getLoadBtn() {
        return loadBtn;
    }

    @Override
    public Button getUnloadBtn() {
        return unloadBtn;
    }

    @Override
    public VMControls getVmControls() {
        return vmControls;
    }

}
